﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cinch;
using System.ComponentModel;
using System.ComponentModel.Composition;

namespace Anata.Client
{
    /// <summary>
    /// Represents a single Image ViewModel
    /// </summary>
    public class KaryawanLayananViewModel : EditableValidatingViewModelBase
    {
        #region Data

        private ViewMode currentViewMode = ViewMode.AddMode;

        #region Data.Primitives
        private DataWrapper<Int32> idKaryawan;
        private DataWrapper<Int32> idLayanan;
        private String namaKaryawan;
        private String namaLayanan;

        private bool isBusy = false;
        #endregion

        private IEnumerable<DataWrapperBase> cachedListOfDataWrappers;

        #region Data.Rules
        #endregion

        #region Data.VMs
        #endregion

        #region Data.Services
        private IMessageBoxService messageBoxService;
        private IUIVisualizerService uiVisualizerService;
        private IReferenceProvider referenceProvider;
        #endregion

        #endregion

        #region Constructor
        [ImportingConstructor]
        public KaryawanLayananViewModel(
            IMessageBoxService messageBoxService,
            IUIVisualizerService uiVisualizerService,
            IReferenceProvider referenceProvider)
        {
            #region Constructor.SetupServices
            this.messageBoxService = messageBoxService;
            this.uiVisualizerService = uiVisualizerService;
            this.referenceProvider = referenceProvider;
            #endregion

            #region Constructor.SetupCommands
            LayananSelectCommand = new SimpleCommand<Object, Object>(CanExecuteLayananSelectCommand, ExecuteLayananSelectCommand);
            KaryawanSelectCommand = new SimpleCommand<Object, Object>(CanExecuteKaryawanSelectCommand, ExecuteKaryawanSelectCommand);
            KaryawanLayananDeleteCommand = new SimpleCommand<Object, Object>(CanExecuteKaryawanLayananDeleteCommand, ExecuteKaryawanLayananDeleteCommand);
            #endregion

            #region Constructor.CreateDataWrappers
            IdKaryawan = new DataWrapper<Int32>(this, idKaryawanChangeArgs);
            IdKaryawan.IsEditable = true;
            IdLayanan = new DataWrapper<Int32>(this, idLayananChangeArgs);
            IdLayanan.IsEditable = true;
            #endregion

            //fetch list of all DataWrappers, so they can be used again later without the
            //need for reflection
            cachedListOfDataWrappers =
                DataWrapperHelper.GetWrapperProperties<KaryawanLayananViewModel>(this);

            #region Create Validation Rules
            #endregion

            #region Constructor.SetupCollections
            #endregion
        }
        #endregion

        #region Static
        static KaryawanLayananViewModel() {
        }
        #endregion

        #region Public Properties

        #region Public.Commands
        public SimpleCommand<Object, Object> KaryawanSelectCommand { get; private set; }
        public SimpleCommand<Object, Object> LayananSelectCommand { get; private set; }
        public SimpleCommand<Object, Object> KaryawanLayananDeleteCommand { get; private set; }
        #endregion

        #region Public.ViewMode
        /// <summary>
        /// The current ViewMode, when changed will loop
        /// through all nested DataWrapper objects and change
        /// their state also
        /// </summary>

        static PropertyChangedEventArgs currentViewModeChangeArgs =
            ObservableHelper.CreateArgs<KaryawanLayananViewModel>(x => x.CurrentViewMode);

        public ViewMode CurrentViewMode
        {
            get { return currentViewMode; }
            set
            {
                currentViewMode = value;

                switch (currentViewMode)
                {
                    case ViewMode.AddMode:
                        //Quantity.DataValue= 0;
                        this.DisplayName = "Transaksi Baru";
                        break;
                    case ViewMode.EditMode:
                        this.DisplayName = "Ubah Transaksi";
                        break;
                    case ViewMode.ViewOnlyMode:
                        this.DisplayName = "Lihat Transaksi";
                        break;
                }

                //Now change all the CachedListOfDataWrappers
                //Which sets all the Cinch.DataWrapper<T>s to the correct IsEditable
                //state based on the new ViewMode applied to the ViewModel
                //we can use the Cinch.DataWrapperHelper class for this
                DataWrapperHelper.SetMode(
                    CachedListOfDataWrappers,
                    currentViewMode);

                NotifyPropertyChanged(currentViewModeChangeArgs);
            }
        }
        #endregion

        #region Public.Data
        /// <summary>
        /// </summary>
        static PropertyChangedEventArgs idKaryawanChangeArgs =
            ObservableHelper.CreateArgs<KaryawanLayananViewModel>(x => x.IdKaryawan);
        public DataWrapper<Int32> IdKaryawan
        {
            get { return idKaryawan; }
            set
            {
                idKaryawan = value;
                NotifyPropertyChanged(idKaryawanChangeArgs);
            }
        }
        /// <summary>
        /// </summary>
        static PropertyChangedEventArgs idLayananChangeArgs =
            ObservableHelper.CreateArgs<KaryawanLayananViewModel>(x => x.IdLayanan);
        public DataWrapper<Int32> IdLayanan
        {
            get { return idLayanan; }
            set
            {
                idLayanan = value;
                NotifyPropertyChanged(idLayananChangeArgs);
            }
        }
        /// <summary>
        /// </summary>
        static PropertyChangedEventArgs namaLayananChangeArgs =
            ObservableHelper.CreateArgs<KaryawanLayananViewModel>(x => x.NamaLayanan);
        public String NamaLayanan
        {
            get { return namaLayanan; }
            set
            {
                namaLayanan = value;
                NotifyPropertyChanged(namaLayananChangeArgs);
            }
        }
        /// <summary>
        /// </summary>
        static PropertyChangedEventArgs namaKaryawanChangeArgs =
            ObservableHelper.CreateArgs<KaryawanLayananViewModel>(x => x.NamaKaryawan);
        public String NamaKaryawan
        {
            get { return namaKaryawan; }
            set
            {
                namaKaryawan = value;
                NotifyPropertyChanged(namaKaryawanChangeArgs);
            }
        }
        #endregion

        #region Public.IsBusy
        /// <summary>
        /// </summary>
        static PropertyChangedEventArgs isBusyChangeArgs =
            ObservableHelper.CreateArgs<KaryawanLayananViewModel>(x => x.IsBusy);
        public bool IsBusy
        {
            get { return isBusy; }
            set
            {
                isBusy = value;
                NotifyPropertyChanged(isBusyChangeArgs);
            }
        }
        #endregion

        #region Public.Cached Collection
        /// <summary>
        /// Returns cached collection of DataWrapperBase
        /// </summary>
        public IEnumerable<DataWrapperBase> CachedListOfDataWrappers
        {
            get { return cachedListOfDataWrappers; }
        }
        #endregion

        #region Public.DataVM
        #endregion

        #region Public.SummaryData
        #endregion

        #endregion

        #region Command Handlers
        /// <summary>
        /// </summary>
        private bool CanExecuteLayananSelectCommand(Object args)
        {
            return !isBusy && CurrentViewMode != ViewMode.ViewOnlyMode;
        }
        /// <summary>
        /// </summary>
        private void ExecuteLayananSelectCommand(Object args)
        {
            LayananSelectViewModel lsvm = new LayananSelectViewModel(
                messageBoxService,
                uiVisualizerService,
                referenceProvider);
            bool? result = uiVisualizerService.ShowDialog("LayananSelectPopup", lsvm);
            if (result.HasValue && result.Value)
            {
                LayananThumbnailViewModel ltvm = (LayananThumbnailViewModel)
                    lsvm.LayanansCV.CurrentItem;
                this.IdLayanan.DataValue = ltvm.IdLayanan;
                this.NamaLayanan = ltvm.Nama;
            }
        }

        /// <summary>
        /// </summary>
        private bool CanExecuteKaryawanSelectCommand(Object args)
        {
            return !isBusy && CurrentViewMode != ViewMode.ViewOnlyMode;
        }
        /// <summary>
        /// </summary>
        private void ExecuteKaryawanSelectCommand(Object args)
        {
            KaryawanSelectViewModel ksvm = new KaryawanSelectViewModel(
                messageBoxService,
                uiVisualizerService,
                referenceProvider);
            ksvm.CriteriaSortIdLayanan = 0;
            ksvm.StartLoading();
            bool? result = uiVisualizerService.ShowDialog("KaryawanSelectPopup", ksvm);
            if (result.HasValue && result.Value)
            {
                KaryawanThumbnailViewModel ktvm = (KaryawanThumbnailViewModel)
                    ksvm.KaryawansCV.CurrentItem;
                this.IdKaryawan.DataValue = ktvm.IdKaryawan;
                this.NamaKaryawan = ktvm.Nama;
            }
        }

        /// <summary>
        /// </summary>
        private bool CanExecuteKaryawanLayananDeleteCommand(Object args)
        {
            return !isBusy && CurrentViewMode != ViewMode.ViewOnlyMode;
        }
        /// <summary>
        /// </summary>
        private void ExecuteKaryawanLayananDeleteCommand(Object args)
        {
            Mediator.Instance.NotifyColleagues("TryRemoveKaryawanLayanan", this);
        }
        #endregion

        #region Change Callbacks
        #endregion

        #region Private Methods
        #endregion

        #region Overrides
        static PropertyChangedEventArgs isValidChangeArgs =
            ObservableHelper.CreateArgs<KaryawanLayananViewModel>(x => x.IsValid);

        public override bool IsValid
        {
            get
            {
                //return base.IsValid and use DataWrapperHelper, if you are
                //using DataWrappers
                return base.IsValid &&
                    DataWrapperHelper.AllValid(cachedListOfDataWrappers) &&
                    IsIdValid;
            }

        }

        public bool IsIdValid
        {
            get
            {
                return IdKaryawan.DataValue != 0 || IdLayanan.DataValue != 0;
            }
        }

        public bool IsDirty
        {
            get
            {
                return DataWrapperHelper2.SomeDirty(cachedListOfDataWrappers);
            }
        }

        public override string ToString()
        {
            return String.Format("KaryawanLayananViewModel {0}", this.idKaryawan);
        }
        #endregion

        #region EditableValidatingViewModelBase overrides
        protected override void OnBeginEdit()
        {
            base.OnBeginEdit();
            //Now walk the list of properties in the OrderViewModel
            //and call BeginEdit() on all Cinch.DataWrapper<T>s.
            //we can use the Cinch.DataWrapperHelper class for this
            DataWrapperHelper.SetBeginEdit(cachedListOfDataWrappers);
        }

        /// <summary>
        /// Override hook which allows us to also put any child 
        /// EditableValidatingViewModelBase objects into the EndEdit state
        /// </summary>
        protected override void OnEndEdit()
        {
            base.OnEndEdit();
            //Now walk the list of properties in the OrderViewModel
            //and call CancelEdit() on all Cinch.DataWrapper<T>s.
            //we can use the Cinch.DataWrapperHelper class for this
            DataWrapperHelper.SetEndEdit(cachedListOfDataWrappers);
        }

        /// <summary>
        /// Override hook which allows us to also put any child 
        /// EditableValidatingViewModelBase objects into the CancelEdit state
        /// </summary>
        protected override void OnCancelEdit()
        {
            base.OnCancelEdit();
            //Now walk the list of properties in the OrderViewModel
            //and call CancelEdit() on all Cinch.DataWrapper<T>s.
            //we can use the Cinch.DataWrapperHelper class for this
            DataWrapperHelper.SetCancelEdit(cachedListOfDataWrappers);
        }
        #endregion
    }
}
